import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import * as path from 'path';
import { resolve } from 'path';
import {loadEnv} from 'vite';
import Components from 'unplugin-vue-components/vite';
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'


const getViteEnv = (mode: string, target: string) => {
  return loadEnv(mode, process.cwd())[target];
};

// https://vitejs.dev/config/
export default ({ mode }) =>
  defineConfig({
    server: {
      host: '0.0.0.0',
      port: getViteEnv(mode, 'VITE_API_PORT') as unknown as number,
      proxy: {
        '/api': {
          target: getViteEnv(mode, 'VITE_APP_API_BASE_URL'),
          changeOrigin: true,
            rewrite: (path) => path.replace(/^\/api/, ""),
        },
          '/api1': {
              target: getViteEnv(mode, 'VITE_APP_API_BASE_URL_1'),
              changeOrigin: true,
              rewrite: (path) => path.replace(/^\/api/, ""),
          },
      },
    },
    plugins: [
      vue(),
      Components({
        resolvers: [AntDesignVueResolver({ importStyle: 'less' })],
      }),

    ],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src')
      },
    },
    css: {
      preprocessorOptions: {
        less: {
          modifyVars: {
            hack: `true; @import (reference) "${resolve("src/assets/style/index.less")}";`,
          },
          javascriptEnabled: true,

        }
      }
    },
      define: {
        __VUE_I18N_FULL_INSTALL__: true,
          __VUE_I18N_LEGACY_API__: true,
          __INTLIFY_PROD_DEVTOOLS__: false,
          __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: true
      },
  });
